﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface.Admin;
using NetDon.OA.Interface.Configuration;
using NetDon.OA.Portal.Constants;
using NetDon.Utilities;
using NineRays.WebControls;

namespace NetDon.OA.Portal.Components
{
    public partial class OrganizationTree : System.Web.UI.UserControl
    {
        public event Action<object, EventArgs> LoadItems;
        public Guid DepartmentId
        {
            get { return ViewState["DepartmentId"] != null ? (Guid)ViewState["DepartmentId"] : Guid.Empty; }

            set { ViewState["DepartmentId"] = value; }
        }

        public Guid? CompanyId
        {
            get { return ViewState["CompanyId"] != null ? (Guid?)ViewState["CompanyId"] : null; }

            set
            {
                ViewState["CompanyId"] = value == Guid.Empty ? null : value;
            }
        }
        public Unit Width
        {
            get { return treeCompany.Width; }
            set { treeCompany.Width = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();

            }
        }
        protected void btnLoad_Click(object sender, EventArgs e)
        {
            if (this.treeCompany.SelectedNode.Tag.ToString() == NodeType.Root.ToString())
            {
                CompanyId = null;
                DepartmentId = Guid.Empty;

            }
            else if (this.treeCompany.SelectedNode.Tag.ToString() == NodeType.Company.ToString())
            {
                CompanyId = this.treeCompany.SelectedNode.Value.ToGuid();
                DepartmentId = Guid.Empty;
            }
            else if (this.treeCompany.SelectedNode.Tag.ToString() == NodeType.Department.ToString())
            {
                DepartmentId = this.treeCompany.SelectedNode.Value.ToGuid();
                var service = ServiceContainer.GetService<IDepartmentService>();
                CompanyId = service.GetDepartment(DepartmentId).CompanyId;

            }
            if (LoadItems != null)
                LoadItems(null, null);

        }

        private void BindControls()
        {
            BindCompany();

        }

        private void BindCompany()
        {
            var service = ServiceContainer.GetService<ICompanyService>();
            var rootCategories = service.GetRootCompanies();
            treeCompany.Nodes.Clear();
            //add root company
            treeCompany.Nodes.Add(CreateRootNode());
            for (int i = 0; i < rootCategories.Count; i++)
            {
                treeCompany.Nodes[0].ChildNodes.Add(CreateCompanyTreeNode(rootCategories[i]));
            }
            treeCompany.Nodes[0].Text =
                Server.HtmlEncode(SystemConfiguration.RootCompanyName);
            treeCompany.Nodes[0].PopulateNodesOnDemand = rootCategories.Count > 0 ? true : false;
            treeCompany.SelectedNode = treeCompany.Nodes[0];

        }
        private void LoadChildNodes(FlyTreeNode node)
        {
            if (node.ChildNodes.Count <= 0)
            {
                //load companies
                var service = ServiceContainer.GetService<ICompanyService>();
                if (node.Tag.ToString() != NodeType.Department.ToString() && !string.IsNullOrEmpty(node.Tag.ToString()))
                {
                    var childCompanies = (CompanyDataCollection)service.GetCompany(node.Value.ToGuid()).GetChildren();
                    for (int i = 0; i < childCompanies.Count; i++)
                    {
                        node.ChildNodes.Add(CreateCompanyTreeNode(childCompanies[i]));

                    }
                }


                ////load deparments
                var childDepartments = ServiceContainer.GetService<IDepartmentService>().GetRootDepartments(node.Value.ToGuid());
                for (int i = 0; i < childDepartments.Count; i++)
                {
                    node.ChildNodes.Add(CreateDepartmentTreeNode(childDepartments[i]));

                }
            }
        }
        protected void treeCompany_PopulateNodes(object sender, FlyTreeNodeEventArgs e)
        {
          
            if (e.Node.ChildNodes.Count <= 0)
            {
                LoadChildNodes(e.Node);
            }
        }
        private FlyTreeNode CreateRootNode()
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(SystemConfiguration.RootCompanyName),
                            Guid.Empty.ToString())
            {
                NodeTypeID = NodeType.Root.ToString(),
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Expanded = true,
                ExpandLevel = 1,
                Tag = NodeType.Root.ToString()
            };
        }
        private FlyTreeNode CreateCompanyTreeNode(CompanyData companyData)
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(companyData.CompanyName),
                            companyData.CompanyId.ToString())
            {
                NodeTypeID = NodeType.Company.ToString(),
                PopulateNodesOnDemand = companyData.HasChildren,
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Tag = NodeType.Company.ToString()
            };
        }
        private FlyTreeNode CreateDepartmentTreeNode(DepartmentData departmentData)
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(departmentData.DepartmentName),
                            departmentData.DepartmentId.ToString())
            {
                NodeTypeID = NodeType.Department.ToString(),
                PopulateNodesOnDemand = departmentData.HasChildren,
                ImageUrl = ApplicationImages.DepartNodeClosed,
                SelectedImageUrl = ApplicationImages.DepartNodeOpened,
                Tag = NodeType.Department.ToString()
            };
        }
    }
}